package com.fgsort;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.JSeparator;
import javax.swing.plaf.SeparatorUI;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.SystemColor;

public class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	public MainFrame(String title) {
		super(title);
		
		// Creates a menubar for a JFrame
        JMenuBar menuBar = new JMenuBar();
        menuBar.setBackground(SystemColor.inactiveCaption);
        
        // Add the menubar to the frame
        setJMenuBar(menuBar);
        
        // Define and add two drop down menu to the menubar
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");

        menuBar.add(fileMenu);
        menuBar.add(helpMenu);
		
        // Create and add simple menu item to one of the drop down menu
        JMenuItem newAction = new JMenuItem("New", new ImageIcon(this.getClass().getResource("newfile.png")));
        JMenuItem openAction = new JMenuItem("Open", new ImageIcon(this.getClass().getResource("openfile.png")));
        JMenuItem exitAction = new JMenuItem("Exit", new ImageIcon(this.getClass().getResource("exit.png")));
       
        fileMenu.add(newAction);
        fileMenu.add(openAction);
        fileMenu.add(exitAction);
        
        JMenuItem FgSortHlpAction = new JMenuItem("FgSort", new ImageIcon(this.getClass().getResource("FgSort.png")));
        JMenuItem aboutHlpAction = new JMenuItem("About", new ImageIcon(this.getClass().getResource("about.png")));
        
        helpMenu.add(FgSortHlpAction);
        helpMenu.add(new JSeparator());
        helpMenu.add(new JSeparator());
        helpMenu.add(aboutHlpAction);
        
        //JToolBar toolbar = new JToolBar();

        //ImageIcon xmlicon = new ImageIcon(getClass().getResource("xml-icon.png"));
        //ImageIcon jsicon = new ImageIcon(getClass().getResource("js-icon.png"));

        //JButton btnXmlOpen = new JButton(xmlicon);
        //btnXmlOpen.setToolTipText("open xml file for editing");
        //JButton btnJsOpen= new JButton(jsicon);
        //btnJsOpen.setToolTipText("does nothing for now");
        
        //toolbar.add(btnXmlOpen);
        //toolbar.add(btnJsOpen);
        
		///set layout manager
		getContentPane().setLayout(new BorderLayout());

		///create options panel on lhs
		final PanelFgSort pnlFgSort = new PanelFgSort("Function Group Sorter");
		pnlFgSort.setBackground(SystemColor.inactiveCaption);

		/// create harware check panel
		final PanelHwCheck pnlHwCheck = new PanelHwCheck("SIEMENS HW Config Check");		
		pnlHwCheck.setBackground(SystemColor.inactiveCaption);

		/// create harware check panel
		final PanelSymbolCheck pnlSymbolCheck = new PanelSymbolCheck(this, "Compare");		
		pnlSymbolCheck.setBackground(SystemColor.inactiveCaption);
		
		///create output window on rhs
		final PanelDebug pnlDebug = new PanelDebug("Output");

		// add swing components to content pane
		Container c = getContentPane();
		
		final JTabbedPane tab = new JTabbedPane();
		c.add(tab, BorderLayout.CENTER);
		//c.add(toolbar, BorderLayout.NORTH);
		tab.add("FgSort", pnlFgSort);
		tab.add("HwCheck", pnlHwCheck);
		tab.add("Symbols", pnlSymbolCheck);
		tab.setBackgroundAt(2, new Color(230, 230, 250));
		tab.add("Debug", pnlDebug);
		
		//tab.add(optionPanel, BorderLayout.WEST);
		//tab.add(outputPanel, BorderLayout.CENTER);
		//tab.remove(component)
		
//        btnXmlOpen.addActionListener(new ActionListener() {
//            public void actionPerformed(ActionEvent event) {
//                
//            	tab.add("xml", new JPanel());
//            	tab.removeTabAt("xml");
//            	tab.getName();
//            }
//        });

		
		///send text from FgSort to Debug output
		pnlFgSort.addOptionListener(new OptionListener() {
            public void detailEventOccurred(OptionEvent event) {
                String funcGrp = event.getText();

                pnlDebug.setDisplayOutput(funcGrp);
            }
        });

		///send text from HwCheck to Debug output
		pnlHwCheck.addOptionListener(new OptionListener() {
            public void detailEventOccurred(OptionEvent event) {
                String funcGrp = event.getText();

                pnlDebug.setDisplayOutput(funcGrp);
            }
        });

		
		///send text from pnlSymbolCheck to Debug output
		pnlSymbolCheck.addOptionListener(new OptionListener() {
            public void detailEventOccurred(OptionEvent event) {
                String funcGrp = event.getText();

                pnlDebug.setDisplayOutput(funcGrp);
            }
        });
		
		///send text from pnlSymbolCheck to Debug output
//		pnlSymbolCheck.addOptionListener(new ProgressListener() {
//            public void detailEventOccurred(OptionEvent event) {
//                String funcGrp = event.getText();
//
//                pnlDebug.setDisplayOutput(funcGrp);
//            }
//        });
		

        newAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	JOptionPane.showMessageDialog(MainFrame.this,"new tab editor window open, functionality not programmed yet","Under Development",JOptionPane.ERROR_MESSAGE);
            }
        });
        
        openAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	JOptionPane.showMessageDialog(MainFrame.this,"open xml file, functionality not programmed yet","Under Development",JOptionPane.ERROR_MESSAGE);
            }
        });

        exitAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        		System.exit(0);
            }
        });
        
        FgSortHlpAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	JOptionPane.showMessageDialog(MainFrame.this,"1.) create a text file that contains unsorted function groups\n     this can be exported from a layout usually\n" +
            												 "2.) edit .xml file to contain desired function groups to sort\n" + 
            												 "3.) select the .xml file shown under the 'Configuration' heading\n" + 
            												 "4.) select the unsorted function group file shown under the 'Unsorted Function Group file' heading\n" + 
            												 "5.) use the 'PLC Option' to separate the function groups into separate plc excel tabs\n" + 
            												 "6.) press 'Sort' to generate Excel Function Group list"
            												 ,"FgSort Help",JOptionPane.INFORMATION_MESSAGE);
            }
        });
        
        aboutHlpAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        		JOptionPane.showMessageDialog(MainFrame.this,"Author: PK\nPackage: iTool (Daimler)\nVersion: 0.5","About",JOptionPane.INFORMATION_MESSAGE);	
        	}
        });
        
       

	}
}
